package com.my.jli.controller;

import com.my.jli.Service.ExerciseService;
import com.my.jli.Service.HomeworkService;
import com.my.jli.Service.StudentService;
import com.my.jli.Service.TeacherService;
import com.my.jli.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

/**
 * @ClassName TeacherController
 * @Description
 * @Author MaYu
 * @Date 2022/1/27 19:49
 * @Version 1.0
 **/
@Controller
public class TeacherController {

    private TeacherService teacherService;
    private StudentService studentService;
    private ExerciseService exerciseService;
    private HomeworkService homeworkService;

    @Autowired
    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }
    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
    @Autowired
    public void setExerciseService(ExerciseService exerciseService) {
        this.exerciseService = exerciseService;
    }
    @Autowired
    public void setHomeworkService(HomeworkService homeworkService) {
        this.homeworkService = homeworkService;
    }

    @RequestMapping("/manage")
    public String login(HttpSession session){

        if (session.getAttribute("teacher") == null){
            return "login";
        } else {
            return "manage/manage";
        }
    }

    @RequestMapping("/teacherLogout")
    public String logout(HttpSession session){
        session.removeAttribute("teacher");
        return "login";
    }

    @RequestMapping("/toManageStu")
    public String toManageStudent(){
        return "manage/manage_stu";
    }

    @RequestMapping("/toAddStu")
    public String toAddStu(){
        return "manage/manage_stu_add";
    }

    @RequestMapping("/toManageSign")
    public String toManageSign(){
        return "manage/manage_sign";
    }

    @RequestMapping("/toManageResource")
    public String toManageResource(){
        return "manage/manage_resource";
    }

    @RequestMapping("/toManageExercise")
    public String toManageExercise(){
        return "manage/manage_exercise";
    }

    @RequestMapping("/toManageHomework")
    public String toManageHomework(){
        return "manage/manage_homework";
    }

    @RequestMapping("/toManageHomeworkDetail")
    public String toManageHomeworkDetail(Integer hid, Model model){
        Homework homework = homeworkService.getById(hid);
        model.addAttribute("homework", homework);
        return "manage/manage_homework_detail";
    }

    @RequestMapping("/toAddHomework")
    public String addHomework(){
        return "manage/manage_homework_add";
    }

    @RequestMapping("/toAddExercise")
    public String toAddExercise(){
        return "manage/manage_exercise_add";
    }

    @RequestMapping("/toAddChapter")
    public String toAddChapter(){
        return "manage/manage_exercise_addChapter";
    }

    @RequestMapping("/toUpdateExercise")
    public String toUpdateExercise(Integer id, Model model){
        Exercise exercise = exerciseService.findExerciseById(id);
        //System.out.println("exercise = " + exercise);
        if (exercise != null){
            ExerciseOption option = exerciseService.findOptionByEid(exercise.getId());
            List<ExerciseType> typeList = exerciseService.findAllType();
            List<ExerciseChapter> chapterList = exerciseService.findAllChapter();

            model.addAttribute("exercise", exercise);
            model.addAttribute("option", option);
            model.addAttribute("typeList", typeList);
            model.addAttribute("chapterList", chapterList);
        }
        return "manage/manage_exercise_update";
    }

    @RequestMapping("/toUpdateStudent")
    public String toUpdateStudent(Integer id, Model model){
        Student student = studentService.getById(id);
        if (student != null){
            Set<String> allGrade = studentService.getAllGrade();
            Set<String> allClazz = studentService.getAllClazz();

            model.addAttribute("student", student);
            model.addAttribute("gradeList", allGrade);
            model.addAttribute("clazzList", allClazz);
        }
        return "manage/manage_stu_update";
    }

    @RequestMapping("/toManageScore")
    public String toManageScore(){
        return "manage/manage_score";
    }
}
